home *** CD-ROM | disk | FTP | other *** search
/ Amiga Plus 2004 #2 / Amiga Plus CD - 2004 - No. 02.iso / AmigaPlus / Tools / Development / MCC_TheBar / Developer / C / Examples / b2chunky / brc1.c < prev    next >
Encoding:
C/C++ Source or Header  |  2004-01-31  |  2.9 KB  |  129 lines

  1.  
  2. #include <exec/types.h>
  3.  
  4. /***********************************************************************/
  5.  
  6. #define DUMP     0
  7. #define RUN      1
  8. #define MINRUN   3
  9. #define MAXRUN 128
  10. #define MAXDAT 128
  11.  
  12. #define PutByte(c)    {*destination++ = (c); *putSize += 1;}
  13. #define UPutByte(c)   (*dest++ = (c))
  14. #define OutDump(nn)   dest = BRCPutDump(dest,nn)
  15. #define OutRun(nn,cc) dest = BRCPutRun(dest,nn,cc)
  16.  
  17. /***********************************************************************/
  18.  
  19. static char
  20. *BRCPutDump(char *destination,int number,char *buf,ULONG *putSize)
  21. {
  22.     int i;
  23.  
  24.     PutByte(number-1);
  25.     for(i = 0; i<number; i++) PutByte(buf[i]);
  26.  
  27.     return destination;
  28. }
  29.  
  30. /***********************************************************************/
  31.  
  32. static char
  33. *BRCPutRun(char *destination,int number,int count,ULONG *putSize)
  34. {
  35.     PutByte(-(number-1));
  36.     PutByte(count);
  37.  
  38.     return destination;
  39. }
  40.  
  41. /***********************************************************************/
  42.  
  43. ULONG
  44. BRCPack(char *pSource,char *pDest,LONG rowSize)
  45. {
  46.     char   buf[256], *source, *dest, c, lastc;
  47.     ULONG  putSize;
  48.     USHORT mode = DUMP;
  49.     SHORT  nbuf, rstart;
  50.  
  51.     source = pSource;
  52.     dest   = pDest;
  53.  
  54.     if (!source) return NULL;
  55.  
  56.     rstart  = 0;
  57.     putSize = 0;
  58.     buf[0]  = lastc = c = *source++;
  59.     nbuf    = 1;
  60.     rowSize--;
  61.  
  62.     for( ; rowSize; --rowSize)
  63.     {
  64.         buf[nbuf++] = c = *source++;
  65.  
  66.         switch(mode)
  67.         {
  68.             case DUMP:
  69.                 if (nbuf>MAXDAT)
  70.                 {
  71.                     dest = BRCPutDump(dest,nbuf-1,buf,&putSize);
  72.                     buf[0] = c;
  73.                     nbuf   = 1;
  74.                     rstart = 0;
  75.  
  76.                     break;
  77.                 }
  78.  
  79.                 if (c==lastc)
  80.                 {
  81.                     if (nbuf-rstart>=MINRUN)
  82.                     {
  83.                         if (rstart>0) dest = BRCPutDump(dest,rstart,buf,&putSize);
  84.                         mode = RUN;
  85.                     }
  86.                     else
  87.                         if (rstart==0)
  88.                         {
  89.                             mode = RUN;
  90.                         }
  91.                 }
  92.                 else
  93.                 {
  94.                     rstart = nbuf-1;
  95.                 }
  96.  
  97.                 break;
  98.  
  99.             case RUN:
  100.                 if ((c!=lastc) || (nbuf-rstart>MAXRUN))
  101.                 {
  102.                     dest = BRCPutRun(dest,nbuf-1-rstart,lastc,&putSize);
  103.                     buf[0] = c;
  104.                     nbuf   = 1;
  105.                     rstart = 0;
  106.                     mode   = DUMP;
  107.                 }
  108.                 break;
  109.         }
  110.  
  111.         lastc = c;
  112.     }
  113.  
  114.     switch(mode)
  115.     {
  116.         case DUMP:
  117.             BRCPutDump(dest,nbuf,buf,&putSize);
  118.             break;
  119.  
  120.         case RUN:
  121.             BRCPutRun(dest,nbuf-rstart,lastc,&putSize);
  122.             break;
  123.     }
  124.  
  125.     return putSize;
  126. }
  127.  
  128. /***********************************************************************/
  129.